package com.byt.framlib.commonwidget;

import android.content.Context;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.byt.framlib.R;
import com.byt.framlib.baseadapter.lv.LvCommonAdapter;
import com.byt.framlib.baseadapter.lv.LvViewHolder;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.byt.framlib.entity.InsideShare;

import java.util.ArrayList;
import java.util.List;

public class CommonInsideShareView extends LinearLayout {

    private Context mContext;
    private View view;

    private TextView tv_statistics_share;
    private NoScrollGridView gv_common_share;
    private List<InsideShare> mShares = new ArrayList<>();
    private LvCommonAdapter<InsideShare> mAdapter;
    private InsideShareItemClick mShareItemClick;

    public CommonInsideShareView(Context context) {
        this(context, null);
        this.mContext = context;
    }

    public CommonInsideShareView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
    }

    public CommonInsideShareView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context);
    }

    private void initView(Context mContext) {
        view = LayoutInflater.from(mContext).inflate(R.layout.layout_common_share, this, true);
        tv_statistics_share = view.findViewById(R.id.tv_statistics_share);
        gv_common_share = view.findViewById(R.id.gv_common_share);
        gv_common_share.setNumColumns(5);
        gv_common_share.setVerticalSpacing(20);
        gv_common_share.setHorizontalSpacing(30);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                (int) getResources().getDimension(R.dimen.x120) * 5 + (int) getResources().getDimension(R.dimen.y30) * (5 - 1), LinearLayout.LayoutParams.WRAP_CONTENT);
        gv_common_share.setLayoutParams(layoutParams); //使设置好的布局参数应用到控件

        mAdapter = new LvCommonAdapter<InsideShare>(mContext, mShares, R.layout.item_common_share_list) {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            protected void convert(LvViewHolder holder, InsideShare share, int position) {
                holder.setImageDrawable(R.id.img_common_share_pic, share.getRes());
                holder.getConvertView().setOnClickListener(v -> {
                    if (AntiShake.check(v.getId())) {
                        return;
                    }
                    if (mShareItemClick != null) {
                        mShareItemClick.onItemClick(v, share);
                    }
                });
            }
        };
        gv_common_share.setAdapter(mAdapter);
    }

    public void setTvTip(CharSequence text) {
        if (tv_statistics_share != null) {
            tv_statistics_share.setText(text);
        }
    }

    public void initDefaultData() {
        if (!mShares.isEmpty()) {
            mShares.clear();
        }
        mShares.add(new InsideShare(1, mContext.getResources().getDrawable(R.drawable.icon_common_xmxb)));
        mShares.add(new InsideShare(2, mContext.getResources().getDrawable(R.drawable.icon_common_wechat)));
        mShares.add(new InsideShare(3, mContext.getResources().getDrawable(R.drawable.icon_common_wechatmoments)));
        mShares.add(new InsideShare(4, mContext.getResources().getDrawable(R.drawable.icon_common_qq)));
        mShares.add(new InsideShare(5, mContext.getResources().getDrawable(R.drawable.icon_common_qzone)));
        mAdapter.notifyDataSetChanged();
    }

    public void setShareItemClick(InsideShareItemClick shareItemClick) {
        this.mShareItemClick = shareItemClick;
    }

    public interface InsideShareItemClick {

        void onItemClick(View view, InsideShare share);
    }
}